<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class ModelTable extends MY_AdminModel
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('session');
    }

    public function get($ID=null, $active = null, $delete = null,$field = false)
    {
        $condition = array();
        if($ID!==null){
            $condition[$this->map->table()->ID]=$ID;
        }
        if($active!==null){
            $condition[$this->map->table()->Activated]=$active?1:0;
        }
        if($delete!==null){
            $condition[$this->map->table()->Deleted]=$delete?1:0;
        }
        $query = $this->db
            ->where($condition)
            ->order_by($this->map->table()->Order, 'ASC')
            ->get($this->map->table)->result_array();
        if($field===true){
            $ci =&get_instance();
            $ci->load->model(MY_AdminController::MODEL_FOLDER.'modelField');
            foreach($query as $k=>$table){
                $query[$k][$this->map->field] = $ci->modelField->get(null,$table[$this->map->table()->ID],null,null);
            }
        }
        return $query;
    }

    public function checkExist($name)
    {
        $query = $this->db->get_where($this->map->table, array($this->map->table()->Name => $name));
        return ($query->num_rows() > 0);
    }

    public function add($name, $displayName, $description)
    {
        if (empty($name)) {
            return $this->lang->line('error_table_name_empty');
        }
        if (preg_match('/[^a-z0-9_]/', $name)) {
            return $this->lang->line('error_table_name_invalid');
        }
        if ($this->checkExist($name)) {
            return $this->lang->line('error_table_name_exist');
        }
        $query = $this->db->get($this->map->table);
        $count = $query->num_rows();
        $userInfo = $this->session->userdata(MY_AdminController::USER_INFO);
        $query = $this->db->insert($this->map->table, array(
            $this->map->table()->Name => $name,
            $this->map->table()->DisplayName => empty($displayName) ? $name : $displayName,
            $this->map->table()->Description => $description,
            $this->map->table()->Order => $count,
            $this->map->table()->Created => time(),
            $this->map->table()->UserID => $userInfo[$this->map->user()->UserID],
            $this->map->table()->Activated => 1,
            $this->map->table()->Deleted => 0
        ));
        return $query;
    }

    public function edit($id, $displayName, $description)
    {
        if (empty($displayName)) {
            return $this->lang->line('error_table_name_empty');
        }
        $query = $this->db->update($this->map->table,
            array(
                $this->map->table()->DisplayName => $displayName,
                $this->map->table()->Description => $description
            ),
            array($this->map->table()->ID => $id));
        return $query;
    }

    public function delete($id, $trash = true)
    {
        if ($trash) {
            $query = $this->db->update($this->map->table, array($this->map->table()->Deleted => 1), array($this->map->table()->ID => $id));
        } else {
            $this->db->delete($this->map->field, array($this->map->field()->TableID => $id));
            $query = $this->db->delete($this->map->table, array($this->map->table()->ID => $id));
        }
        return $query;
    }

    public function active($id, $active = true)
    {
        $query = $this->db->update($this->map->table, array($this->map->table()->Activated => $active ? 1 : 0), array($this->map->table()->ID => $id));
        return $query;
    }
}